package week8;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

/**
 * @author The Setting sun
 * @create 2021-10-28 16:44
 */
public class FileEncode {//文件加密器
    JFrame jf;
    JLabel l1, l2;
    JFileChooser fc;
    JTextField tf;
    JCheckBox cb;
    JButton butt1, butt2;

    public FileEncode() {
        jf = new JFrame("文件加密/解密器");
        l1 = new JLabel("选择加密/解密的文件");
        l2 = new JLabel("选择加密/解密算子（0-255）");
        fc = new JFileChooser("");
        tf = new JTextField(15);
        cb = new JCheckBox("覆盖原文件");
        butt1 = new JButton("加密/解密");
        butt2 = new JButton("取消");
    }

    public void init() {
        JPanel p1 = new JPanel();
        JPanel p2 = new JPanel();
        p1.setLayout(new FlowLayout(FlowLayout.LEFT));//设置左对齐
        p1.add(l1);
        fc.setControlButtonsAreShown(false);//设置控制按钮隐藏
        p1.add(fc);
        p1.add(l2);
        p1.add(tf);
        p1.add(cb);
        eventHandle1();
        p2.add(butt1);
        eventHandle2();
        p2.add(butt2);
        jf.add(p1, BorderLayout.CENTER);
        jf.add(p2, BorderLayout.SOUTH);
        jf.setSize(600, 520);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setLocationRelativeTo(null);
        jf.setVisible(true);
    }

    public void eventHandle1() {
        butt1.addActionListener(new ActionListener() {//加密按钮添加事件

            public void actionPerformed(ActionEvent e) {
                //获取选择的文件、加密算子、是否覆盖3个数据
                File file = fc.getSelectedFile();
                int code = Integer.parseInt(tf.getText());
                boolean selected = cb.isSelected();

                if (selected == true) {//覆盖源文件，用随机读写类
                    try {
                        RandomAccessFile raf = new RandomAccessFile(file, "rw");
                        int b;//逐个字节进行读取，加密/解密后 写入
                        while ((b = raf.read()) != -1) {
                            raf.seek(raf.getFilePointer() - 1);
                            raf.write(b ^ code);
                        }

                        raf.close();
                    } catch (FileNotFoundException e1) {
                        e1.printStackTrace();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                } else {
                    try {
                        FileInputStream in = new FileInputStream(file);
                        //加密/解密目标文件
                        FileOutputStream out = new FileOutputStream(new File("D:\\worktest\\code\\codefiel1.txt"));

                        int b;//逐个字节进行读取，加密后 写入
                        while ((b = in.read()) != -1) {
                            out.write(b ^ code);
                        }

                        out.close();
                        in.close();
                    } catch (FileNotFoundException e1) {
                        e1.printStackTrace();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }

                JDialog dialog = new JDialog(jf, "加密/解密成功");
                dialog.setLayout(new FlowLayout(FlowLayout.CENTER));
                JLabel label = new JLabel("文件加密/解密成功！");
                label.setFont(new Font("楷体", Font.BOLD, 24));
                dialog.add(label);
                JButton b = new JButton("确定");
                b.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        dialog.dispose();
                    }
                });
                dialog.add(b);
                dialog.setLocationRelativeTo(jf);
                dialog.setSize(200, 200);
                dialog.setVisible(true);
            }
        });
    }

    public void eventHandle2() {//取消按钮事件
        butt2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
    }

    public static void main(String[] args) {
        new FileEncode().init();
    }
}

